﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace OpenComposite.EII.Designer.Workflow.Forms
{
	public partial class FormEditAliases : Form
	{
		#region Initialization
		private FormEditAliases()
		{
			_event = null;
			_method = null;

			InitializeComponent();

			this.DialogResult = DialogResult.Cancel;

			dgvAliasesPaths.AutoGenerateColumns = false;
		}
		public FormEditAliases(Repository.WorkflowMethod wfmethod)
			: this()
		{
			_method = wfmethod;
		}
		public FormEditAliases(Repository.WorkflowEvent wfevent)
			: this()
		{
			_event = wfevent;
		}
		#endregion

		#region Private Members

		#region Fields
		private Repository.WorkflowMethod _method;
		private Repository.WorkflowEvent _event;
		#endregion

		#region Methods
		private void initCorrelationAliases(
			List<OpenComposite.EII.Repository.CorrelationAlias> list,
			OpenComposite.EII.Repository.Workflow workflow)
		{
			foreach ( var item in workflow.ExtendedProperties.CorrelationParameters ) {
				if ( !list.Exists(
					delegate(Repository.CorrelationAlias alias)
					{
						return alias.ParameterName == item;
					}) ) {
					list.Add(new OpenComposite.EII.Repository.CorrelationAlias(item));
				}
			}
		}

		#endregion

		#region Event Handlers
		private void FormEditAliases_Load(object sender, EventArgs e)
		{
			if ( _method != null ) {
				lblName.Text = "Method:";
				txtName.Text = _method.Display;
				initCorrelationAliases(_method.ExtendedProperties.CorrelationAliases, _method.Workflow);
				dgvAliasesPaths.DataSource = _method.ExtendedProperties.CorrelationAliases;
			} else if ( _event != null ) {
				lblName.Text = "Event:";
				txtName.Text = _event.Display;
				initCorrelationAliases(_event.ExtendedProperties.CorrelationAliases, _event.Workflow);
				dgvAliasesPaths.DataSource = _event.ExtendedProperties.CorrelationAliases;
			}
		}

		private void tsbtnOK_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void tsbtnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		private void dgvAliasesPaths_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			if ( e.RowIndex < 0 || e.ColumnIndex != colEdit.Index ) return;
			Repository.CorrelationAlias alias = dgvAliasesPaths.Rows[e.RowIndex].DataBoundItem as Repository.CorrelationAlias;
			Repository.BusinessObject bobj = null;
			if ( _method != null ) {
				bobj = _method.InputBusinessObject;
			} else if ( _event != null ) {
				bobj = _event.BusinessObject;
			}
			if ( alias == null || bobj == null ) return;
			FormSelectBObjectPath frm = new FormSelectBObjectPath(bobj);
			if ( frm.ShowDialog() == DialogResult.OK ) {
				alias.Path = frm.SelectedPath;
				dgvAliasesPaths.UpdateCellValue(colPath.Index, e.RowIndex);
			}
		}
		private void dgvAliasesPaths_CellClick(object sender, DataGridViewCellEventArgs e)
		{

		}

		#endregion

		#endregion

	}
}
